#!/bin/bash

# Configure Global`s Variables
VERSION="0.0.10"
NAME="SlackConf GUI for Slackware Configuration"
DIA="/bin/dialog"
BKP_OPT="Y"

# Function check_depends
function check_depends()
{
	#Check if user run is 'root'
	USER_RUN=`whoami`
        if [ $USER_RUN != "root" ]; then
                echo -e ''
                echo -e '*** Please only run as root user ***'
                echo -e ''
                echo -e ''
                exit_
        fi

}

# Function for exit program
function exit_()
{
	#Exit program
	echo -e "$NAME"
	echo -e "========================================="
	echo -e ""
	echo -e "bye ... ;-)"
	echo -e ""
	echo -e ""
	exit 0;
}

# Function for display information about program
function about()
{
	$DIA	\
	--title "$NAME - $VERSION"	\
	--msgbox "\n $NAME, version $VERSION.
		-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- \n\n\n
		slackconf is a tool for setting up a Slackware Linux System. \n\n
		You can help by reporting an error, correcting some function, or by \n
		sending ideas in the solution can be useful. \n\n
		Or if you want to submit your comment, please write to: \n\n
		contato@brunorusso.eti.br \n\n\n
		http://linux.brunorusso.eti.br/doku.php?id=slackconf \n\n\n
		Slackware (R) is a registered trademark of Patrick Volkerding, for more \n 
		information visit: http://slackware.com/trademark/trademark.php "	\
	30 90
	main_menu
}

#Function Conf_Network
Conf_Network()
{
	#run netconfig
	netconfig
	main
}

#Function Conf_Mouse
Conf_Mouse()
{
	#run mouseconfig
	mouseconfig
	main
}

#Function Conf_Packages
Conf_Packages()
{
	#run pkgtool
	pkgtool
	main
}

#Function BKP_FILE
function BKP_FILE()
{
	cp $1 $1.bkp	
}

#Function Conf_Runlevel
function Conf_Runlevel()
{
	FILE_INITTAB="/etc/inittab"
	#Detect runlevel configurated
	RUNLEVEL=`cat /etc/inittab | grep id: | cut -d":" -f2`
	RUNLEVEL_OPTION=$( $DIA \
		--stdout \
		--title "$NAME - $VERSION"	\
		--inputbox "Your level configured is $RUNLEVEL, but you can change. \n\n \
		\n0	halt
		\n1	single user mode
		\n2	unused (but configured the same as runlevel 3)
		\n3	multiuser mode (default Slackware runlevel)
		\n4	X11 with KDM/GDM/XDM (session managers)
		\n5	unused (but configured the same as runlevel 3)
		\n6	reboot \n" 0 0 )

	#If cancel return is 1	
	if [ $? = 1 ]; then
		main
	fi

	#if opntion is equal runlevel, nothing
	if [ $RUNLEVEL_OPTION == $RUNLEVEL ]; then
		main;
	fi

	#Case value not 0-6 reload this function
	if [[ $RUNLEVEL_OPTION != [0-6] ]]; then
		Conf_Runlevel
	fi

	if [[ $RUNLEVEL_OPTION = [0-6] ]]; then
		RUNLEVEL_OPTION_YN=$($DIA	\
			--stdout	\
			--title "$NAME - $VERSION"	\
			--yesno "Runlevel is changed to $RUNLEVEL_OPTION, ok?"	\
			0 0	\
		)
		if [ $? = 0 ]; then
			#change file
			if [ $BKP_OPT == "Y" ]; then
				BKP_FILE "$FILE_INITTAB"
			fi
			sed "s/id:$RUNLEVEL:initdefault:/id:$RUNLEVEL_OPTION:initdefault:/g" $FILE_INITTAB > /tmp/INITTAB.tmp && cp /tmp/INITTAB.tmp $FILE_INITTAB && rm /tmp/INITTAB.tmp
			main
		else
			#return to conf_Runlevel 
			Conf_Runlevel
		fi
	fi

}

#Function Conf_Services
function Conf_Services()
{
	SERVICE_OPT=$($DIA --stdout	\
		--title "$NAME - $VERSION"	\
		--menu 'Do you need?'	\
		0 0 0	\
		Enables	'a Service'	\
		Disables 'a Service')

        if [ $? = 1 ]; then
                main_menu
        fi
	if [ $SERVICE_OPT == "Disables" ]; then
		#Identifies the executable scripts
		find /etc/rc.d/ -perm -400 | grep -v "rc.[0-6]" | grep -v "rc.[K,M,S]" | grep -v "init.d" | grep -v "rc[0-9]" | cut -d"/" -f4 | sort > /tmp/.services.slackconf00
		cp /tmp/.services.slackconf00 /tmp/.services.slackconf01
		paste /tmp/.services.slackconf00 /tmp/.services.slackconf01 -d" " > /tmp/.services.slackconf
		#cat /tmp/.services.slackconf02 | sed 's/$/ ON/' > /tmp/.services.slackconf
		escolha=$($DIA --stdout	\
			--title "$NAME - $VERSION"	\
			--menu "What service you want to disable?"	\
			0 0 0 	\
			$(cat /tmp/.services.slackconf))
		chmod -x /etc/rc.d/$escolha
		Conf_Services
	fi
	#Identifies non executable scripts
	find /etc/rc.d/ -perm -400 | grep -v "rc.[0-6]" | grep -v "rc.[K,M,S]" | grep -v "init.d" | grep -v "rc[0-9]" | cut -d"/" -f4 | sort > /tmp/.services.slackconf00
	cp /tmp/.services.slackconf00 /tmp/.services.slackconf01
	paste /tmp/.services.slackconf00 /tmp/.services.slackconf01 -d" " > /tmp/.services.slackconf
	#cat /tmp/.services.slackconf02 | sed 's/$/ ON/' > /tmp/.services.slackconf
	escolha=$($DIA --stdout	\
		--title "$NAME - $VERSION"	\
		--menu "What service you want to enable?"	\
		0 0 0 	\
		$(cat /tmp/.services.slackconf))
	chmod +x /etc/rc.d/$escolha
	Conf_Services
}

main_menu()
{
	OPTION=$($DIA --stdout  \
		--title "$NAME - $VERSION"      \
		--menu 'Which option would you like?'   \
		0 0 0                                     \
		RunLevel     'Configure runlevel startup' \
		Services     'Enable or desable startup script'      \
		Packages     'Manage Packages using pkgtool'      \
		Mouse	     'Configure mouse using mouseconfig'      \
		Network      'Configure network using netconfig'      \
		About        "About $NAME"      \
	Exit         "Return to prompt")


        if [ $? = 1 ]; then
                exit_
        fi
        if [ $OPTION == 'RunLevel' ]; then
                Conf_Runlevel
        elif [ $OPTION == "Services" ]; then
                Conf_Services
        elif [ $OPTION == "Packages" ]; then
                Conf_Packages
        elif [ $OPTION == "Network" ]; then
                Conf_Network
        elif [ $OPTION == "Mouse" ]; then
                Conf_Mouse
        elif [ $OPTION == "About" ]; then
                about
        elif [ $OPTION == "Exit" ]; then
                exit_
        fi

}

main()
{
	check_depends
	main_menu
}
main
